package com.shxc.smarttrade.quotation.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.quotation.model.TradeCategory;

import java.util.ArrayList;
import java.util.List;

/**
 * 行情 - 自选 - 下方
 */

public class QuotatonChooseBottomAdapter extends RecyclerView.Adapter<QuotatonChooseBottomAdapter.ViewHolder> {

    private Context mContext;
    private List<TradeCategory.DataBean.SymbolsBean> mList;
    private Handler mHandler;
    private int nums = 0;
    private List<Integer> slist;

    public QuotatonChooseBottomAdapter(Context c, List<TradeCategory.DataBean.SymbolsBean> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
        slist = new ArrayList();
    }

    public void setNums(int n) {
        nums = n;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_quotation_choose_bottom, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final TradeCategory.DataBean.SymbolsBean model = mList.get(position);
//        holder.symbolcn_textview.setText(model.getSymbolCn());
//        holder.symbolen_textview.setText(model.getSymbolEn());
        holder.symbolcn_textview.setText(model.getSymbolEn());
        holder.symbolen_textview.setText(model.getSymbolCn());
        if (model.isFocus()) {
            //被选中
            holder.item_relativelayout.setBackgroundResource(R.drawable.bg_round_quotation_gray);
            holder.symbolcn_textview.setTextColor(mContext.getResources().getColor(R.color.text_quotation_bottom_gray));
            holder.symbolen_textview.setTextColor(mContext.getResources().getColor(R.color.text_quotation_bottom_gray));
        } else {
            //未被选中
            holder.item_relativelayout.setBackgroundResource(R.drawable.box_round_quotation_choose_bottom);
            holder.symbolcn_textview.setTextColor(mContext.getResources().getColor(R.color.main_color));
            holder.symbolen_textview.setTextColor(mContext.getResources().getColor(R.color.text_quotation_choose_gray));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (slist.contains(position)) {
                    return;
                }
                if (nums == 15) {
                    mHandler.sendEmptyMessage(3);
                } else {
                    model.setFocus(true);
                    notifyDataSetChanged();

                    Message msg = new Message();
                    msg.what = 2;
                    msg.obj = model;
                    msg.arg1 = position;
                    mHandler.sendMessage(msg);
                    slist.add(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setList(List<TradeCategory.DataBean.SymbolsBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void setModelForse(String symbolEn) {
        int tt = -1;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getSymbolEn().equals(symbolEn)) {
                mList.get(i).setFocus(false);
                tt = i;
                break;
            }
        }
        for (int i = 0; i < slist.size(); i++) {
            if (slist.get(i) == tt) {
                slist.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected RelativeLayout item_relativelayout;//中文
        protected TextView symbolcn_textview;//中文
        protected TextView symbolen_textview;//英文

        public ViewHolder(View itemView) {
            super(itemView);
            item_relativelayout = (RelativeLayout) itemView.findViewById(R.id.item_relativelayout);
            symbolcn_textview = (TextView) itemView.findViewById(R.id.symbolcn_textview);
            symbolen_textview = (TextView) itemView.findViewById(R.id.symbolen_textview);
        }
    }

}
